<template>
    <div ref="dragDiv" class="drag">
        <div ref="drag_bg" class="drag_bg" />
        <div ref="drag_text" class="drag_text">{{ confirmWords }}</div>
        <!-- <div class="slide-check-tip">请滑动验证</div> -->
        <div ref="moveDiv" :class="{ 'handler_ok_bg': confirmSuccess }" class="handler handler_bg"
            style="position: absolute;top: 0px;" @touchstart="mousedownFn"  />

    </div>
</template>

<script>
export default {
    data() {
        return {
            beginClientX: 1, /* 距离屏幕左端距离 */
            mousePressing: false, /* 是否正则按压滑动条 */
            maxwidth: 380, /* 拖动最大宽度，依据滑块宽度算出来的 */
            confirmWords: '按住滑块，拖动到最右边', /* 滑块文字 */
            confirmSuccess: false /* 验证成功判断 */
        };
    },
    mounted() {
        document.getElementsByTagName('html')[0].addEventListener('touchmove', this.mouseMoveFn);
        document.getElementsByTagName('html')[0].addEventListener('touchend', this.moseUpFn);
        document.getElementsByTagName('html')[0].addEventListener('touchstart', this.mousedownFn);
        this.maxwidth = document.body.clientWidth
    },
    methods: {
        mousedownFn: function (e) {
            console.log(' mousedownFn ', e);
            // alert(JSON.stringify(e))
            if (e.isTrusted) {
                this.initMaxWidth();
                if (!this.confirmSuccess) {  
                    e.preventDefault && e.preventDefault(); // 阻止文字选中等 浏览器默认事件
                    this.mousePressing = true;
                    // this.maxwidth = e.touches[0].screenX
                    this.beginClientX = e.touches[0].screenX;
                }
            }
        }, // mousedoen 事件
        successFunction() {
            this.confirmSuccess = true;
            this.confirmWords = '验证通过';
            if (window.addEventListener) {
                document.getElementsByTagName('html')[0].removeEventListener('touchmove', this.mouseMoveFn);
                document.getElementsByTagName('html')[0].removeEventListener('touchstart', this.mouseStartFn);

                document.getElementsByTagName('html')[0].removeEventListener('touchend', this.moseUpFn);
            } else {
                document.getElementsByTagName('html')[0].removeEventListener('touchend', () => { });
            }
            this.$refs.drag_text.style.color = '#fff';
            // this.$refs.moveDiv.style.right = 0 + 'px';
            this.$refs.moveDiv.style.left = this.maxwidth + 'px';

            this.$refs.drag_bg.style.width = this.maxwidth + 'px';
            this.$emit('success', this.confirmSuccess);
        }, // 验证成功函数
        mouseMoveFn(e) {
            // alert(JSON.stringify(e.touches[0].clientX))
            if(e.isTrusted){
                if (this.mousePressing) {
                console.log('mouseMoveFn', e.touches[0])
                const width = Number.parseInt(e.changedTouches[0].screenX) - this.beginClientX;
                // alert(JSON.stringify(this.maxwidth))

                //   console.log(this.$refs.moveDiv.style.left)
                console.log('max', width)
                if (width > 0 && width <= this.maxwidth) {
                    this.$refs.moveDiv.style.left = width + 'px';
                    this.$refs.drag_bg.style.width = width + 'px';
                    console.log(this.$refs.moveDiv.style.left)
                } else if (width > this.maxwidth) {
                    // alert(JSON.stringify(e.changedTouches[0]))
                    this.successFunction();
                }
            }
            }
            
        }, // mousemove事件
        moseUpFn(e) {
            console.log('moseUpFn', e);
            this.mousePressing = false;
            var width = e.changedTouches[0].screenX - this.beginClientX;
            if (width < this.maxwidth) {
                // document.getElementsByClassName('handler')[0].style.left = 0 + 'px'
                // document.getElementsByClassName('drag_bg')[0].style.width = 0 + 'px'
                this.$refs.moveDiv.style.left = 0 + 'px';
                this.$refs.drag_bg.style.width = 0 + 'px';
            }
        }, // mouseup事件,
        initMaxWidth() {

            this.maxwidth = this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
            // this.maxwidth = 338
        },
        // 将滑动条重新至于最左侧
        reset() {
            this.$refs.moveDiv.style.left = 0 + 'px';
            this.$refs.drag_bg.style.width = 0 + 'px';
            this.$refs.drag_text.style.color = '#333';
            this.confirmSuccess = false;
            this.mousePressing = false;
            // this.maxwidth = this.$refs.dragDiv.clientWidth - this.$refs.moveDiv.clientWidth;
            document.getElementsByTagName('html')[0].addEventListener('mousemove', this.mouseMoveFn);
            document.getElementsByTagName('html')[0].addEventListener('mouseup', this.moseUpFn);
        }
    }
};
</script>

<style scoped>
.slide-check-tip {
    position: absolute;
    display: inline-block;
    
    top: 0px;
    right: -85px;
    /* border: 1px solid red; */
    color: #999;
    font-size: smaller;
}

.drag {
    box-sizing: border-box;
    margin: 10px 50px;
    position: relative;
    background-color: #e8e8e8;
    /* width: 100%; */
    height: 100px;
    line-height: 100px;
    text-align: center;
}

.handler {
    width: 70px;
    height: 98px;
    border: 1px solid #ccc;
    cursor: move;
}

.handler_bg {
    background: #fff url('') no-repeat center;
}

.handler_ok_bg {
    background: #fff url('') no-repeat center;
}

.drag_bg {
    background-color: #7ac23c;
    height: 100px;
    width: 0;
}

.drag_text {
    position: absolute;
    top: 0px;
    width: 100%;
    color: #333;
    text-align: center;
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none;
    -o-user-select: none;
    -ms-user-select: none;
}
</style>